package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.KnowDetailsModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ShareUtil;
import com.banshuibao.bsb_android.util.TextPaintUtils;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.WebViewCssUtil;

public class KnowledgeDetailsActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTitle, mAuthor, mSource, mLook, mDate;
    private WebView webView;
    private ImageView back, shared;
    private int flag;
    private GetLoginInfo getLoginInfo;
    private String uuid;
    private KnowDetailsModel.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge_details);
        initView();
        uuid = getIntent().getStringExtra("uuid");
        //flag: 1知识库传来  2案例库传来
        flag = getIntent().getIntExtra("flag", 0);
        if (!TextUtils.isEmpty(uuid) && flag == 1) {
            showProgressDialog();
            setPost(uuid);
        } else if (!TextUtils.isEmpty(uuid) && flag == 2) {
            showProgressDialog();
            setPostTwo(uuid);
        }
        getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
    }

    //案例库详情接口请求
    private void setPostTwo(String uuid) {
        KnowDetailsModel.body body = new KnowDetailsModel.body();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.CASE_DETAILS, body, new HttpResponse(this, KnowDetailsModel.class) {
            @Override
            public void onResult(Object obj) {
                KnowDetailsModel s = (KnowDetailsModel) obj;
//                LogUtil.e(s);
                 data = s.getData();
                setData(data);
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }

    //知识库详情接口请求
    private void setPost(String uuid) {
        KnowDetailsModel.body body = new KnowDetailsModel.body();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.KNOWLEDGE_DETAILS, body, new HttpResponse(this, KnowDetailsModel.class) {
            @Override
            public void onResult(Object obj) {
                KnowDetailsModel s = (KnowDetailsModel) obj;
//                LogUtil.e(s);
                 data = s.getData();
                setData(data);
            }


            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }

    private void setData(KnowDetailsModel.DataBean data) {
        if (!TextUtils.isEmpty(data.getTitle())) {
            mTitle.setText(data.getTitle());
            TextPaintUtils.textMothed(mTitle);
        }
        if (!TextUtils.isEmpty(data.getAuthor())) {
            mAuthor.setText("【作者】 " + data.getAuthor());
        }
        if (!TextUtils.isEmpty(data.getSource())) {
            mSource.setText("【来源】 " + data.getSource());
        }
        mLook.setText(String.valueOf(data.getClickNum()));
        String time = null;
        if (data.getCreateDate() != 0) {
            time = TimeUtil.format(data.getCreateDate());
        } else {
            time = "";
        }
        mDate.setText(time);
        String newHtml = WebViewCssUtil.setCss(data.getContent());
        webView.loadDataWithBaseURL(null, newHtml, "text/html", "utf-8", null);
    }

    private void initView() {
        back = findViewById(R.id.back_details);
        shared = findViewById(R.id.details_shared);
        mTitle = findViewById(R.id.details_title);
        mAuthor = findViewById(R.id.details_author);
        mSource = findViewById(R.id.details_source);
        mLook = findViewById(R.id.details_look);
        mDate = findViewById(R.id.details_date);
        webView = findViewById(R.id.details_web);
        //   WebViewUtil.addImageClickListener(this, webView);
        back.setOnClickListener(this);
        shared.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_details:
                finish();
                break;
            case R.id.details_shared:
                switch (flag) {
                    case 1:
                        ShareUtil.showShare(KnowledgeDetailsActivity.this,Constants.Url.KNOWLEDGE+"?uuid="+uuid+"&firmId="+getLoginInfo.getFirmId()+"&inviteUserId="+getLoginInfo.getUserId(),data.getTitle(),"",null);
                        break;
                    case 2:
                        ShareUtil.showShare(KnowledgeDetailsActivity.this,Constants.Url.CASE+"?uuid="+uuid+"&firmId="+getLoginInfo.getFirmId()+"&inviteUserId="+getLoginInfo.getUserId(),data.getTitle(),"",null);
                        break;
                }
                break;
        }
    }
}
